package net.realsee.service.wcs.device.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.device.WcsPlcDevice;
import net.realsee.domain.wcs.device.bo.WcsPlcDeviceBo;
import net.realsee.domain.wcs.device.vo.WcsPlcDeviceVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.device.WcsPlcDeviceMapper;
import net.realsee.service.wcs.device.IWcsPlcDeviceService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * IOT设备Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsPlcDeviceServiceImpl extends ServiceImpl<WcsPlcDeviceMapper, WcsPlcDevice> implements IWcsPlcDeviceService {

    private final WcsPlcDeviceMapper baseMapper;

    /**
     * 查询IOT设备
     */
    @Override
    public WcsPlcDeviceVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询IOT设备列表
     */
    @Override
    public TableDataInfo<WcsPlcDeviceVo> selectVoPage(WcsPlcDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsPlcDevice> lqw = buildVoQueryWrapper(bo);
        Page<WcsPlcDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询IOT设备列表
     */
    @Override
    public List<WcsPlcDeviceVo> selectVoList(WcsPlcDeviceBo bo) {
        LambdaQueryWrapper<WcsPlcDevice> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsPlcDevice> buildVoQueryWrapper(WcsPlcDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsPlcDevice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsPlcDevice::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDevice()), WcsPlcDevice::getDevice, bo.getDevice());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), WcsPlcDevice::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), WcsPlcDevice::getChannel, bo.getChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getCommType()), WcsPlcDevice::getCommType, bo.getCommType());
        lqw.eq(StringUtils.isNotBlank(bo.getCommAddr()), WcsPlcDevice::getCommAddr, bo.getCommAddr());
        lqw.eq(bo.getCommPort() != null, WcsPlcDevice::getCommPort, bo.getCommPort());
        lqw.eq(StringUtils.isNotBlank(bo.getIotAddr()), WcsPlcDevice::getIotAddr, bo.getIotAddr());
        lqw.eq(bo.getStatus() != null, WcsPlcDevice::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceType()), WcsPlcDevice::getDeviceType, bo.getDeviceType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceVender()), WcsPlcDevice::getDeviceVender, bo.getDeviceVender());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceModel()), WcsPlcDevice::getDeviceModel, bo.getDeviceModel());
        lqw.eq(StringUtils.isNotBlank(bo.getOperationType()), WcsPlcDevice::getOperationType, bo.getOperationType());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsPlcDevice::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsPlcDevice::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsPlcDevice::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsPlcDevice::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsPlcDevice::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsPlcDevice::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsPlcDevice::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsPlcDevice::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsPlcDevice::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsPlcDevice::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsPlcDevice::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsPlcDevice::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsPlcDevice::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增IOT设备
     */
    @Override
    public Boolean insertByBo(WcsPlcDeviceBo bo) {
        WcsPlcDevice add = BeanUtil.toBean(bo, WcsPlcDevice.class);
        return this.save(add);
    }

    /**
     * 修改IOT设备
     */
    @Override
    public Boolean updateByBo(WcsPlcDeviceBo bo) {
        WcsPlcDevice update = BeanUtil.toBean(bo, WcsPlcDevice.class);
        return this.updateById(update);
    }

    /**
     * 批量删除IOT设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 通过设备编号获取设备信息（会缓存）
     */
    @Override
    public WcsPlcDevice getByDevice(String device) {
        return null;
    }

    /**
     * 清除设备缓存（任何修改和删除操作，都要调用该方法）
     */
    @Override
    public void cleanDeviceCache() {

    }
}
